package dongze.com.ckdj.dagger.module;

import android.content.Context;

import com.google.gson.GsonBuilder;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import dongze.com.ckdj.BuildConfig;
import dongze.com.ckdj.dagger.component.ServiceComponent;
import dongze.com.ckdj.data.DataLayer;
import dongze.com.ckdj.data.HttpLogger;
import dongze.com.ckdj.data.gson.DoubleDefault0Adapter;
import dongze.com.ckdj.data.gson.GsonArrayNullAdapter;
import dongze.com.ckdj.data.gson.IntegerDefault0Adapter;
import dongze.com.ckdj.data.gson.LongDefault0Adapter;
import dongze.com.ckdj.data.webinterface.BackEndInterface;
import dongze.com.ckdj.data.webinterface.ImageUploadInterface;
import dongze.com.ckdj.map.MapUtil;
import dongze.com.ckdj.persistence.dao.DaoMaster;
import dongze.com.ckdj.persistence.dao.DaoSession;
import io.reactivex.Scheduler;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by fanqwz on 2017/8/11.
 */

@Module
public class ServiceModule {
    private static final int MAXTASKRUNNING = 5;
    public static int TIME_OUT = BuildConfig.DEBUG ? 60 : 30;
    private Context context;

    public ServiceModule(Context context) {
        this.context = context;
    }

    @Provides
    @Singleton
    Retrofit.Builder getRetrofitBuilder() {
        return new Retrofit.Builder()
                .baseUrl(BackEndInterface.BASE_URL)
                .client(new OkHttpClient.Builder()
                        .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
                        .readTimeout(TIME_OUT, TimeUnit.SECONDS)
                        .writeTimeout(TIME_OUT, TimeUnit.SECONDS)
//                        .addInterceptor(new HttpLoggingInterceptor().setLevel(
//                                BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE))
                        .addInterceptor(new HttpLogger())
                        .build())
                .addConverterFactory(GsonConverterFactory.create(new GsonBuilder()
                        .setDateFormat("yyyy-MM-dd HH:mm:ss")
//                        .serializeNulls()
                        .registerTypeAdapter(Integer.class, new IntegerDefault0Adapter())
                        .registerTypeAdapter(int.class, new IntegerDefault0Adapter())
                        .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(Long.class, new LongDefault0Adapter())
                        .registerTypeAdapter(long.class, new LongDefault0Adapter())
                        .registerTypeAdapter(List.class, new GsonArrayNullAdapter())
                        .create()));
    }

    @Provides
    @Singleton
    BackEndInterface getBackEndInterface(Retrofit.Builder builder) {
        return builder.baseUrl(BackEndInterface.BASE_URL)
                .build()
                .create(BackEndInterface.class);
    }

    @Provides
    @Singleton
    ImageUploadInterface getImageUploadInterface(Retrofit.Builder builder) {
        return builder.baseUrl(ImageUploadInterface.BASE_URL)
                .build()
                .create(ImageUploadInterface.class);
    }

    @Provides
    @Singleton
    Scheduler getScheduler() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(MAXTASKRUNNING, MAXTASKRUNNING, 5, TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(1024));
        return Schedulers.from(executor);
    }

    @Singleton
    @Provides
    MapUtil getMapUtil(ServiceComponent serviceComponent) {
        return new MapUtil(serviceComponent);
    }

    @Singleton
    @Provides
    DaoSession getDaoSession() {
        // FIXME: 2017/8/21 devOpenHelper will drop all data when update
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(context, "image.db");
        DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDb());
        return daoMaster.newSession();
    }
}
